<!--
 * @Description: 角色管理
 * @Author: yang
 * @Date: 2021-06-24 17:20:10
 * @LastEditors: Morgan
 * @LastEditTime: 2021-07-02 20:00:31
-->
<template>
  <div>
    <div>
      <el-table :data="tableData" :title="title" stripe style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" />
        <el-table-column prop="name" label="角色名称" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.prevent="toGrant(scope.row)"
            >授权</el-button>
            <el-button
              type="text"
              size="small"
              class="blue_info"
              @click.prevent="toEdit(scope.row)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <!-- 编辑里的模态框 -->
      <el-dialog :title="title" :visible.sync="visible">
        <el-form
          ref="rule_Form"
          :model="ruleForm"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="toSave">确定</el-button>
            <el-button @click="visible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!-- 授权模态框 -->
      <el-dialog title="授权" :visible.sync="grant_visible" width="80%">
        <el-form :model="grant">
          <el-form-item label="角色名称" label-width="80px">
            {{ grant.name }}</el-form-item>
          <el-form-item label="权限" label-width="80px">
            <el-cascader-panel
              v-model="grant.privileges"
              :options="options"
              :props="props"
              clearable
            />
          </el-form-item>
          <div>
            <el-button type="primary" @click="Savegrant">确定</el-button>
            <el-button @click="grant_visible = false">取消</el-button>
          </div>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  cascadePrivilegeFindAll,
  authorization,
  saveOrUpdate
} from '@/api/role'
import { findPrivilegeTree } from '@/api/privilege'
export default {
  data() {
    return {
      tableData: [],

      ruleForm: {},
      visible: false,
      grant_visible: false,
      grant: {},
      title: '添加角色信息',
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      },
      props: {
        multiple: true,
        value: 'id',
        label: 'name',
        emitPath: false,
        checkStrictly: true
      },
      options: []
    }
  },
  computed: {},
  created() {
    this.queryData()
    this.loadPrivileges()
  },

  mounted() {},
  methods: {
    // 获取数据
    async queryData() {
      const res = await cascadePrivilegeFindAll()
      // 将权限转换为id的数组
      res.data.forEach((item) => {
        item.privileges = item.privileges.map((index) => {
          return index.id
        })
      })
      this.tableData = res.data
    },
    toEdit(row) {
      // 在角色名称栏显示该角色角色名
      this.ruleForm = { ...row }
      // 显示标题
      this.title = '编辑角色信息'
      this.visible = true
    },
    // 保存编辑信息
    toSave() {
      this.$refs['rule_Form'].validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.ruleForm)
          this.visible = false
          this.$message({ message: res.message, type: 'success' })
          this.queryData()
        } else {
          return false
        }
      })
    },
    // 保存授权信息
    async Savegrant() {
      if (this.grant.privileges.length == 0) {
        this.grant.privileges = ''
      }
      const res = await authorization(this.grant)
      this.grant_visible = false
      this.$message({ message: res.message, type: 'success' })
      this.queryData()
    },
    // 模态框与授权按钮绑定
    toGrant(record) {
      this.grant = record
      this.grant_visible = true
    },

    // 获取授权树
    async loadPrivileges() {
      // this.loading = true;
      const res = await findPrivilegeTree()
      this.foo(res.data)
      this.options = res.data
      // this.loading = false;
    },
    /**
     * @description:  递归去除权限中的空children
     * @param {*} privileges
     * @return {*}
     */
    foo(privileges) {
      for (const p of privileges) {
        if (p.children && p.children.length === 0) {
          delete p.children
        } else {
          this.foo(p.children)
        }
      }
    }
  }
}
</script>
<style scoped>
</style>
